from django.shortcuts import get_object_or_404, render_to_response
from farmers.crops.models import Crop_Type, Market, Selling
from django.http import HttpResponse

def index(request):

    if request.method == 'POST':
#        string = request.META["QUERY_STRING"]
        string = request.POST.items()
    elif request.method == 'GET':
#        string =  request.META["QUERY_STRING"]
#        query = request.GET.get('query')
        query = request.GET.get('').split()[1]
        crop = Crop_Type.objects.filter(name_english__contains=query)
        price = Market.objects.filter(crop__name_english__contains=query)
        selling = Selling.objects.filter(crop__name_english__contains=query)
        string = "Year: Market | Selling  Price per kg (" + query + ")\n"
        i = 0
        s = []
#        selling = list(enumerate(selling))
        for item in price:
            sel = selling[i]
            s.append((item,sel))
            i = i + 1

        for item,paid in s:
            string += str(item.year) + ": " + str(item.price) + "  |  " + str(paid.price) +  "\n"
            
        
    else:
        string = "error"

    return HttpResponse(string)    
    
            
#    return render_to_response('friends/index.html', {'friends_list': friends_list})

